<template>
  <div class="banner-container">
    <div class="banner-top">
      <div class="banner-search"></div>
      <div class="banner-btn">
        <el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button>
      </div>
    </div>
    <el-scrollbar style="height: calc(100vh - 190px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list">
        <el-table-column label="id" prop="data_id" width="80"></el-table-column>
        <el-table-column label="艺术家名" prop="art_name" width="100"></el-table-column>
        <el-table-column label="作品名称" prop="work_name" width="100"></el-table-column>
        <el-table-column label="关于类型" width="100">
          <template slot-scope="scope">
            <span>{{ aboutType[scope.row.about_type] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="类型" width="60">
          <template slot-scope="scope">
            <span>{{ type[scope.row.type] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="大图/视频" width="120">
          <template slot-scope="subScope">
            <el-image
              v-if="Array.isArray(subScope.row.opus_id) && subScope.row.opus_id.length > 0"
              style="width: 100px; height: 40px; cursor: pointer"
              :src="getBigImg(subScope.row.opus_id[0].id)"
              @click="lookBig(subScope.row)"
              fit="cover"
            ></el-image>
            <span v-else-if="subScope.row.video_url">视频已上传</span>
            <span v-else>未上传</span>
          </template>
        </el-table-column>
        <el-table-column label="作品介绍" prop="introduction"></el-table-column>
        <el-table-column label="封面" width="120">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.title_page"
              style="width: 100px; height: 40px"
              :src="scope.row.title_page"
              :preview-src-list="[scope.row.title_page]"
              fit="cover"
            ></el-image>
            <span v-else>暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-upload
              action=""
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeAvatarUpload(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">上传封面</el-button>
            </el-upload>
            <el-upload
              v-if="scope.row.type == 2"
              action=""
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeAvatarUploadVideo(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">上传视频</el-button>
            </el-upload>
            <el-upload
              v-if="scope.row.type == 1"
              action="/ws"
              class="avatar-uploader"
              style="width: 90px; float: left"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeAvatarUploadBigImg(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">上传大图</el-button>
            </el-upload>
            <el-button type="primary" @click="updAbout(scope.row.data_id, scope.row)" size="mini">修改</el-button>
            <el-button type="danger" @click="delBanner(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>

    <paging ref="page" :func="getList"></paging>
    <el-dialog @closed="onClose" title="提示" :visible.sync="isShowDialog">
      <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
        <el-form-item label="作品类型" prop="type">
          <el-select v-model="params.type" placeholder="请选择类型" @change="getWorks">
            <template v-for="(item, i) in type">
              <el-option v-if="i != 0" :key="i" :label="item" :value="i"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="关于类型" prop="about_type">
          <el-select v-model="params.about_type" placeholder="请选择类型">
            <template v-for="(item, i) in aboutType">
              <el-option v-if="i != 0" :key="i" :label="item" :value="i"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="艺术家">
          <el-select
            v-model="params.art_name"
            remote
            filterable
            :remote-method="remoteMethod"
            :loading="loading"
            placeholder="请选择艺术家"
            @change="changeArt"
          >
            <el-option v-for="item in arts" :key="item.data_id" :label="item.name" :value="item.data_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作品名称" prop="work_name">
          <el-input v-model="params.work_name"></el-input>
        </el-form-item>
        <el-form-item label="作品简介" prop="introduction">
          <el-input v-model="params.introduction"></el-input>
        </el-form-item>
        <el-form-item label="关联作品">
          <el-select
            v-model="params.x"
            placeholder="请选择作品"
            @change="checkWork"
            remote
            filterable
            :remote-method="refRemoteMethod"
          >
            <el-option
              v-for="item in workList"
              :key="item.data_id"
              :label="item.video_name || item.name"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import paging from '@/components/paging';
  import menuManage from '@/api/menu';
  import { getBigImg } from '@/utils/image';
  import videoManage from '@/api/video';
  import artManage from '@/api/works';
  import aboutWorksManage from '@/api/impression';
  import artisManage from '@/api/artis';
  import { qiniuUploadFile } from '@/utils/QiniuUpload';

  export default {
    name: 'banner-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        menuList: [],
        isShowDialog: false,
        updId: '',
        workList: [],
        params: {
          type: '',
          work_name: '',
          about_type: '',
          art_id: '',
          art_name: '',
          introduction: '',
          title_page: '',
          video_url: '',
          opus_id: [],
        },
        rules: {
          type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        },
        aboutType: ['', '自然景观', '人文景观', '地标建筑', '艺术创作'],
        type: ['', '图片', '视频'],
        arts: [],
        loading: false,
      };
    },
    async mounted() {
      this.refresh();
      this.getMenuList();
      artisManage.getArtis({ notPagination: true }).then((res) => {
        this.arts = res.item_list;
      });
    },
    methods: {
      async refRemoteMethod(query) {
        this.getWorks({ key: query });
      },
      async remoteMethod(query) {
        // if (!query) return;
        this.loading = true;
        let userRes = await artisManage.getArtis({ key: query, notPagination: true });
        this.arts = userRes.item_list;
        this.loading = false;
      },
      // 查看大图
      lookBig(data) {
        console.log(data);
        if (data.opus_id && data.opus_id.length > 0) {
          let { BasicDraw, BigImageShow } = require('../../../utils/main');
          let basicDraw = new BasicDraw(); // 创建基础渲染类
          basicDraw.start(); // 开始渲染
          new BigImageShow().showBigImg(data.opus_id[0].id);

          document.getElementById('bigIamgeClose').addEventListener('click', (e) => {
            basicDraw.dispose();
            e.stopPropagation();
          });
        }
      },
      getBigImg,
      changeArt(e) {
        this.arts.forEach((item) => {
          if (item.data_id === e) {
            this.params.art_name = item.name;
          }
        });
      },
      updAbout(data_id, m) {
        this.updId = data_id;
        this.isShowDialog = true;
        this.params = { ...m };
        this.getWorks();
      },
      async delBanner(data_id) {
        this.$alert('是否确定删除作品', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await aboutWorksManage.delAboutWorks(data_id);
              this.refresh();
            }
          },
        });
      },
      refresh() {
        this.$refs.page.refresh();
      },
      async getMenuList() {
        let res = await menuManage.getMenu();
        this.menuList = res.item_list;
      },
      getList(callback) {
        aboutWorksManage.getAboutWorks().then((res) => {
          let total = res.total_count;
          this.list = res.item_list;
          callback(this.list, total);
        });
      },
      async dialogConfirm() {
        this.$refs.dataParams.validate(async (valid) => {
          if (valid) {
            let res = null;
            if (this.updId) {
              res = await aboutWorksManage.updAboutWorks(this.updId, this.params);
            } else {
              res = await aboutWorksManage.setAboutWorks(this.params);
            }
            if (res) {
              this.isShowDialog = false;
              this.refresh();
            }
          }
        });
      },
      onClose() {
        this.updId = '';
        this.params = {
          text: '',
        };
      },
      async beforeAvatarUpload(file, id) {
        let res = await qiniuUploadFile(file, 'impression');
        if (res) {
          aboutWorksManage.updAboutWorks(id, { title_page: res });
        }
        // const formData = new FormData();
        // formData.append('上传的key值', file);
        // await aboutWorksManage.setAboutWorksImage(id, formData);
        // this.refresh();
      },
      async beforeAvatarUploadVideo(file, id) {
        let res = await qiniuUploadFile(file, 'impression');
        if (res) {
          aboutWorksManage.updAboutWorks(id, { video_url: res });
        }

        // const formData = new FormData();
        // formData.append('上传的key值', file);
        // await aboutWorksManage.setAboutWorksVideo(id, formData);
        // this.refresh();
      },
      async beforeAvatarUploadBigImg(file, id) {
        const formData = new FormData();
        formData.append('上传的key值', file);

        let res = await qiniuUploadFile(file, 'impression');
        if (res) {
          let url = res.key;
          let opus_id = await artManage.imageMap({
            image_list: [
              {
                image_src_url: url,
                image_src_type: 'local',
                image_name: url.split('/')[2],
                type: 1,
                access_type: 0,
                note: '',
              },
            ],
          });
          await aboutWorksManage.updAboutWorks(id, { opus_id });
          this.$message.success('上传成功');
        }
      },
      getWorks(params) {
        let methods = [0, artManage.getArt, videoManage.getVideoType];
        let get = methods[this.params.type];
        get(params).then((res) => {
          this.workList = res.item_list;
        });
      },
      checkWork(m) {
        console.log('m', m);
        this.params.art_name = m.art_name;
        this.params.art_id = m.data_id;
        this.params.introduction = m.work_detail || '';
        this.params.title_page = m.video_page || '';
        this.params.video_url = m.video_path || '';
        this.params.work_name = m.video_name || m.name;
        this.isShowDialog = false;
        this.dialogConfirm();
      },
    },
  };
</script>

<style lang="less" scoped>
  .banner-container {
    ::v-deep {
      .cell {
        display: flex;

        div {
          margin-right: 6px;
        }
      }
    }

    height: 100%;

    .avatar-uploader {
      .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
    }

    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .banner-top {
      display: flex;
      align-items: center;
      justify-content: space-between;

      // margin-bottom: 12px;
      .banner-search {
        display: flex;
      }
    }

    .banner-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;

      .banner-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;

        .item-content {
          margin: 10px;

          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
